/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 25.Ara.2009, 13:58:28
 */
package org.testis.gui.mvc;

import org.testis.gui.mvc.view.WindowLocationProvider;
import org.testis.gui.mvc.controller.DynamicMapControllerList;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapPopupController;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapTreeController;
import org.testis.gui.mvc.controller.testdesign.PopupController;
import org.testis.gui.mvc.controller.testdesign.TreeController;
import org.testis.gui.mvc.model.dynamicmap.DynamicMapPopupModel;
import org.testis.gui.mvc.model.dynamicmap.DynamicMapTreeModel;
import org.testis.gui.mvc.model.testdesign.PopupModel;
import org.testis.gui.mvc.model.testdesign.TreeModel;
import org.testis.gui.mvc.utilities.DebugLogger;
import org.testis.gui.mvc.utilities.ValidationUtils;
import org.testis.gui.mvc.view.dynamicmap.DynamicMapPopupView;
import org.testis.gui.mvc.view.testdesign.ChangeUserPassword;
import org.testis.gui.mvc.view.testdesign.PopupViewPanel;
import org.testis.gui.mvc.view.testdesign.TreeViewPanel;
import org.testis.gui.utils.AtofGuiManifestInfo;
import org.testis.utils.UserSettingsManager;
import org.testis.utils.AtofActiveMqConnectionFactory;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.orm.PersistentException;
import org.orm.cfg.JDBCConnectionSetting;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.utils.middleware.DynamicMap;
import org.testis.utils.middleware.UserSession;

/**
 *
 * @author selcuk
 */
public class MainWindow extends JFrame implements WindowListener {

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();
        System.setOut(DebugLogger.getInstance());
        setTitle(getGuiHeaderText());
        addWindowListener(this);

        TreeModel treeModel = new TreeModel();
        PopupModel popupModel = new PopupModel();
        DynamicMapTreeModel dynamicMapTreeModel = new DynamicMapTreeModel();
        DynamicMapPopupModel dynamicMapPopupModel = new DynamicMapPopupModel();

        TreeController treeController = new TreeController();
        PopupController popupController = new PopupController();
        TestDesignControllerList.newInstance(treeController, popupController,
                this);

        TestBedControllerList.newInstance(this);

        DynamicMapTreeController dynamicMapTreeController = new DynamicMapTreeController();
        DynamicMapPopupController dynamicMapPopupController = new DynamicMapPopupController();
        DynamicMapControllerList.newInstance(dynamicMapTreeController,
                dynamicMapPopupController, this);

        TreeViewPanel treeViewPanel = new TreeViewPanel();
        PopupViewPanel popupViewPanel = new PopupViewPanel();
        DynamicMapPopupView dynamicMapPopupView = new DynamicMapPopupView();

        treeController.addView(treeViewPanel);
        popupController.addView(popupViewPanel);
        dynamicMapTreeController.addView(treeViewPanel);
        dynamicMapPopupController.addView(dynamicMapPopupView);
        treeController.addModel(treeModel);
        popupController.addModel(popupModel);
        dynamicMapTreeController.addModel(dynamicMapTreeModel);
        dynamicMapPopupController.addModel(dynamicMapPopupModel);

        treeModel.initDefault();
        dynamicMapTreeModel.initDefault();

//        getContentPane().add(popupViewPanel);
        pnlContainer.setLayout(new BorderLayout());
        pnlContainer.add(treeViewPanel);

        DynamicMap.newInstance();
        pack();
    }

    public void centerScreen() {
        WindowLocationProvider wlp = new WindowLocationProvider(this);
        wlp.centerScreen();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlContainer = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        filMenu = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();
        testDesignMenu = new javax.swing.JMenu();
        newTestDesignMenuItem = new javax.swing.JMenuItem();
        openTestDesignMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        runTestDesignMenuItem = new javax.swing.JMenuItem();
        compareTestDesignMenuItem = new javax.swing.JMenuItem();
        dynamicMapMenu = new javax.swing.JMenu();
        newDynamicMapMenuItem = new javax.swing.JMenuItem();
        openDynamicMapMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        pageParserDynamicMapMenuItem = new javax.swing.JMenuItem();
        compareDynamicMapMenuItem = new javax.swing.JMenuItem();
        settingsMenu = new javax.swing.JMenu();
        labelSettingsMenuItem = new javax.swing.JMenuItem();
        profileSettingsMenuItem = new javax.swing.JMenuItem();
        languageSettingsMenuItem = new javax.swing.JMenuItem();
        configurationSettingsMenuItem = new javax.swing.JMenuItem();
        passwordSettingMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();
        updateMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        filMenu.setText("File");

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        filMenu.add(exitMenuItem);

        jMenuBar1.add(filMenu);

        testDesignMenu.setText("Test Design");

        newTestDesignMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        newTestDesignMenuItem.setText("New");
        newTestDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newTestDesignMenuItemActionPerformed(evt);
            }
        });
        testDesignMenu.add(newTestDesignMenuItem);

        openTestDesignMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        openTestDesignMenuItem.setText("Open");
        openTestDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openTestDesignMenuItemActionPerformed(evt);
            }
        });
        testDesignMenu.add(openTestDesignMenuItem);
        testDesignMenu.add(jSeparator1);

        runTestDesignMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        runTestDesignMenuItem.setText("Run");
        runTestDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runTestDesignMenuItemActionPerformed(evt);
            }
        });
        testDesignMenu.add(runTestDesignMenuItem);

        compareTestDesignMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        compareTestDesignMenuItem.setText("Compare");
        compareTestDesignMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compareTestDesignMenuItemActionPerformed(evt);
            }
        });
        testDesignMenu.add(compareTestDesignMenuItem);

        jMenuBar1.add(testDesignMenu);

        dynamicMapMenu.setText("Dynamic Map");

        newDynamicMapMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        newDynamicMapMenuItem.setText("New");
        newDynamicMapMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDynamicMapMenuItemActionPerformed(evt);
            }
        });
        dynamicMapMenu.add(newDynamicMapMenuItem);

        openDynamicMapMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        openDynamicMapMenuItem.setText("Open");
        openDynamicMapMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openDynamicMapMenuItemActionPerformed(evt);
            }
        });
        dynamicMapMenu.add(openDynamicMapMenuItem);
        dynamicMapMenu.add(jSeparator2);

        pageParserDynamicMapMenuItem.setText("Page parser");
        pageParserDynamicMapMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pageParserDynamicMapMenuItemActionPerformed(evt);
            }
        });
        dynamicMapMenu.add(pageParserDynamicMapMenuItem);

        compareDynamicMapMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        compareDynamicMapMenuItem.setText("Compare");
        compareDynamicMapMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compareDynamicMapMenuItemActionPerformed(evt);
            }
        });
        dynamicMapMenu.add(compareDynamicMapMenuItem);

        jMenuBar1.add(dynamicMapMenu);

        settingsMenu.setText("Settings");

        labelSettingsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, java.awt.event.InputEvent.ALT_MASK));
        labelSettingsMenuItem.setText("Label settings");
        labelSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                labelSettingsMenuItemActionPerformed(evt);
            }
        });
        settingsMenu.add(labelSettingsMenuItem);

        profileSettingsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, java.awt.event.InputEvent.ALT_MASK));
        profileSettingsMenuItem.setText("Profile settings");
        profileSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileSettingsMenuItemActionPerformed(evt);
            }
        });
        settingsMenu.add(profileSettingsMenuItem);

        languageSettingsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, java.awt.event.InputEvent.ALT_MASK));
        languageSettingsMenuItem.setText("Language settings");
        languageSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                languageSettingsMenuItemActionPerformed(evt);
            }
        });
        settingsMenu.add(languageSettingsMenuItem);

        configurationSettingsMenuItem.setText("Configurations");
        configurationSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configurationSettingsMenuItemActionPerformed(evt);
            }
        });
        settingsMenu.add(configurationSettingsMenuItem);

        passwordSettingMenuItem.setText("Change Password");
        passwordSettingMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordSettingMenuItemActionPerformed(evt);
            }
        });
        settingsMenu.add(passwordSettingMenuItem);

        jMenuBar1.add(settingsMenu);

        jMenu1.setText("Help");

        aboutMenuItem.setText("About");
        jMenu1.add(aboutMenuItem);

        updateMenu.setText("Update");
        updateMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateMenuActionPerformed(evt);
            }
        });
        jMenu1.add(updateMenu);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 717, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlContainer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 543, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        doExit();
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void labelSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_labelSettingsMenuItemActionPerformed
        TestDesignControllerList.newInstance().getLabelSettingsController().operationStartLoader();
    }//GEN-LAST:event_labelSettingsMenuItemActionPerformed

    private void profileSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileSettingsMenuItemActionPerformed
        TestDesignControllerList.newInstance().getProfileSettingsController().
                startLoader();
    }//GEN-LAST:event_profileSettingsMenuItemActionPerformed

    private void languageSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_languageSettingsMenuItemActionPerformed
        TestDesignControllerList.newInstance().getLanguageHandlerController().
                startLoader();
    }//GEN-LAST:event_languageSettingsMenuItemActionPerformed

    private void openTestDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openTestDesignMenuItemActionPerformed
        TestDesignControllerList.newInstance().getLoadTestSuiteController().
                startLoader();
    }//GEN-LAST:event_openTestDesignMenuItemActionPerformed

    private void newTestDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTestDesignMenuItemActionPerformed
        TestDesignControllerList.newInstance().getTestSuiteController().
                operationNewTestSuite();
    }//GEN-LAST:event_newTestDesignMenuItemActionPerformed

    private void newDynamicMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newDynamicMapMenuItemActionPerformed
        DynamicMapControllerList.newInstance().getDynamicMapController().
                operationNewDynamicMap("Container");
    }//GEN-LAST:event_newDynamicMapMenuItemActionPerformed

    private void openDynamicMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openDynamicMapMenuItemActionPerformed
        DynamicMapControllerList.newInstance().getLoadDynamicMapController().
                startLoader();
    }//GEN-LAST:event_openDynamicMapMenuItemActionPerformed

    private void runTestDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runTestDesignMenuItemActionPerformed
        TestDesignControllerList.newInstance().getTreeController().runTestSuite();
    }//GEN-LAST:event_runTestDesignMenuItemActionPerformed

    private void pageParserDynamicMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pageParserDynamicMapMenuItemActionPerformed
        MyDefaultMutableTreeNode node =
                DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationGetSelectedNode();
        if (node != null) {
            DynamicMapControllerList.newInstance().getWebPageParserController().
                    operationNewWebPageParser();
        }

    }//GEN-LAST:event_pageParserDynamicMapMenuItemActionPerformed

    private void compareTestDesignMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compareTestDesignMenuItemActionPerformed
        TestDesignControllerList.newInstance().getTreeController().operationClearSelection();
        TestDesignControllerList.newInstance().getTreeController().
                operationSelectCompareMode(Boolean.TRUE);
    }//GEN-LAST:event_compareTestDesignMenuItemActionPerformed

    private void compareDynamicMapMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compareDynamicMapMenuItemActionPerformed
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationClearSelection();
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                operationSelectCompareMode(Boolean.TRUE);
    }//GEN-LAST:event_compareDynamicMapMenuItemActionPerformed

    private void updateMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateMenuActionPerformed
        UpdateGui f = new UpdateGui(this, true);
        f.setVisible(true);

    }//GEN-LAST:event_updateMenuActionPerformed

    private void configurationSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configurationSettingsMenuItemActionPerformed
        try {
            UserConfigView view = new UserConfigView(this, true);
            view.centerScreen();
            view.setVisible(true);
        } catch (Exception ex) {
            System.out.println("Error:" + ex.getMessage());
        }
    }//GEN-LAST:event_configurationSettingsMenuItemActionPerformed

    private void passwordSettingMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordSettingMenuItemActionPerformed
        ChangeUserPassword changePass = new ChangeUserPassword(new javax.swing.JFrame(), true);
        changePass.centerScreen();
    }//GEN-LAST:event_passwordSettingMenuItemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem compareDynamicMapMenuItem;
    private javax.swing.JMenuItem compareTestDesignMenuItem;
    private javax.swing.JMenuItem configurationSettingsMenuItem;
    private javax.swing.JMenu dynamicMapMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu filMenu;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JMenuItem labelSettingsMenuItem;
    private javax.swing.JMenuItem languageSettingsMenuItem;
    private javax.swing.JMenuItem newDynamicMapMenuItem;
    private javax.swing.JMenuItem newTestDesignMenuItem;
    private javax.swing.JMenuItem openDynamicMapMenuItem;
    private javax.swing.JMenuItem openTestDesignMenuItem;
    private javax.swing.JMenuItem pageParserDynamicMapMenuItem;
    private javax.swing.JMenuItem passwordSettingMenuItem;
    private javax.swing.JPanel pnlContainer;
    private javax.swing.JMenuItem profileSettingsMenuItem;
    private javax.swing.JMenuItem runTestDesignMenuItem;
    private javax.swing.JMenu settingsMenu;
    private javax.swing.JMenu testDesignMenu;
    private javax.swing.JMenuItem updateMenu;
    // End of variables declaration//GEN-END:variables

    private boolean hasLastLoadedTrees() {
        try {
            String val = UserSettingsManager.getInstance().getSelectedSuites();
            if (val != null && val.trim().length() > 0) {
                return true;
            }
            val = UserSettingsManager.getInstance().getSelectedDynamicMaps();
            if (val != null && val.trim().length() > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    private void addLastLoadedTrees(LinkedList<String> suiteList, LinkedList<String> dynamicMapList) {
        try {
            for (int i = 0; i < suiteList.size(); i++) {
                System.out.println("Loading test tree with id:" + suiteList.get(
                        i));
                TestDesignControllerList.newInstance().getTreeController().
                        operationLoadTreeFromDb(suiteList.get(i));
            }

            for (int i = 0; i < dynamicMapList.size(); i++) {
                System.out.println("Loading Dynamic map tree with id:" + dynamicMapList.get(
                        i));
                String[] splitArr = dynamicMapList.get(i).split(" -- ");
                if (splitArr.length == 0) {
                    continue;
                }

                int dynamicMapID = 0;
                try {
                    dynamicMapID = Integer.parseInt(splitArr[0]);
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                    continue;
                }
                DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationLoadTreeFromDatabase(
                        dynamicMapID);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static boolean checkSqlConnection() {
        JDBCConnectionSetting settings = null;
        try {
            settings =
                    org.testis.orm.manager.TestisDbPersistentManager.getJDBCConnectionSetting();
        } catch (PersistentException ex) {
            ex.printStackTrace();
        }
        String dbUrl = "jdbc:mysql://localhost/atof";
        String dbUserName = "root";
        String dbPassword = "12345";
        if (settings != null) {
            dbUrl = settings.getConnectionURL();
            dbUserName = settings.getUserName();
            dbPassword = settings.getPassword();
        }
        return ValidationUtils.checkMySqlConnection(dbUrl,
                dbUserName,
                dbPassword);
    }

    private boolean checkActiveMqConnection() {


        String activemqUrl = "tcp://" + UserSettingsManager.getInstance().getActiveMqUrl() + ":61616?wireFormat.maxInactivityDuration=5000";

        return AtofActiveMqConnectionFactory.checkActiveMqConnection(activemqUrl);
    }

    @Override
    public void windowOpened(WindowEvent e) {
//        m_displayFrame.setEnabled(true);
        if (checkSqlConnection()) {
            if (hasLastLoadedTrees()) {
                TestDesignControllerList.newInstance().getSelectSuiteController().startLoader();
                LinkedList<String> selectedSuites = TestDesignControllerList.newInstance().getSelectSuiteController().getSelectedTestSuites();
                LinkedList<String> selectedDynamicMaps = TestDesignControllerList.newInstance().getSelectSuiteController().getSelectedDynamicMaps();
                addLastLoadedTrees(selectedSuites, selectedDynamicMaps);
            }
        } else {
            JOptionPane.showMessageDialog(null,
                    "Could not connect to mysql server",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        if (!checkActiveMqConnection()) {
            JOptionPane.showMessageDialog(null,
                    "Could not connect to activemq server",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return;
    }

    private String getGuiHeaderText() {
        AtofGuiManifestInfo utils = new AtofGuiManifestInfo();
        String specificationVersion = utils.getSpecificationVersion();
        String implementationVersion = utils.getImplementationVersion();
        return "ATOF Designer GUI  -v" + specificationVersion + " -r" + implementationVersion;
    }

    public static void loadUserDefinedDbDefinitions() {

        String dbUrl = UserSettingsManager.getInstance().getDbUrl();
        String dbUserName = UserSettingsManager.getInstance().getDbUserName();
        String dbPassword = UserSettingsManager.getInstance().getDbPassword();
        String dbName = UserSettingsManager.getInstance().getDbCatalogName();
        JDBCConnectionSetting connectionSetting =
                new JDBCConnectionSetting();
        connectionSetting.setConnectionURL("jdbc:mysql://" + dbUrl + "/"
                + dbName);
        connectionSetting.setDialect("org.hibernate.dialect.MySQLDialect");
        connectionSetting.setDriverClass("com.mysql.jdbc.Driver");

        connectionSetting.setUserName(dbUserName);
        connectionSetting.setPassword(dbPassword);
        try {
            org.testis.orm.manager.TestisDbPersistentManager.setJDBCConnectionSetting(
                    connectionSetting);
        } catch (PersistentException ex) {
            ex.printStackTrace();
        }

    }

    private void doExit() {
        saveUserProps();
        closeDbSession();
        System.exit(0);
    }

    private void saveUserProps() {
        try {
            String[] loadedTestDesigns = TestDesignControllerList.newInstance().
                    getTreeController().getLoadedTrees();
            String[] loadedDynamicMaps = DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().getLoadedTrees();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < loadedTestDesigns.length; i++) {
                if (sb.length() == 0) {
                    sb.append(loadedTestDesigns[i]);
                } else {
                    sb.append("#" + loadedTestDesigns[i]);
                }
            }
            UserSettingsManager.getInstance().setSelectedSuites(sb.toString());
            sb = new StringBuilder();
            for (int i = 0; i < loadedDynamicMaps.length; i++) {
                if (sb.length() == 0) {
                    sb.append(loadedDynamicMaps[i]);
                } else {
                    sb.append("#" + loadedDynamicMaps[i]);
                }
            }
            UserSettingsManager.getInstance().setSelectedDynamicMaps(sb.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void closeDbSession() {
        try {
            TestisDbPersistentManager.instance().getSession().flush();
            TestisDbPersistentManager.instance().getSession().clear();
            TestisDbPersistentManager.instance().getSession().close();
        } catch (PersistentException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void windowClosing(WindowEvent e) {
        doExit();
    }

    @Override
    public void windowClosed(WindowEvent e) {
        closeDbSession();
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

    public static boolean configureUserSettings(boolean showDbSettings) {
        try {
            if (showDbSettings) {
                UserConfigView view = new UserConfigView(null, true);
                   view.centerScreen();
                view.setVisible(true);
            }
        } catch (Exception ex) {
            System.out.println("Error:" + ex.getMessage());
        }
        loadUserDefinedDbDefinitions();
        if (checkSqlConnection()) {
            LoginDialog login = new LoginDialog(new javax.swing.JFrame(), true);
            login.centerScreen();
            if (login.getReturnStatus() == LoginDialog.RET_CANCEL) {
                return false;
            }
            login.dispose();
            return true;

        }
        return false;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        boolean isOpened = false;

        if (configureUserSettings(!UserSettingsManager.getInstance().isDontshowAtStartup()) && UserSession.newInstance().isValidSession()) {
            isOpened = true;
            MainWindow mw = new MainWindow();
            mw.centerScreen();
        } else {
            if (!checkSqlConnection()) {
                if (configureUserSettings(true) && UserSession.newInstance().isValidSession()) {
                    isOpened = true;
                    MainWindow mw = new MainWindow();
                    mw.centerScreen();
                }
            }
        }
        if (!isOpened) {
            System.exit(0);
        }

    }
}
